<?php
/*
 * 足球阵型采集
 */

namespace app\sports\lineup\service;

use think\Db;
use think\Exception;
use app\common\HTTP;
use app\common\OSS;
use think\facade\Log;
use app\sports\lineup\model\LineupDetailModel;
use app\common\InstanceTrait;
class LineupCaiji
{

    /*
     * 足球阵容详细阵容处理
     * @$string  主队或者客队的详细信息
     * @$id    纳米的比赛id
     * @$home  1 主队阵容 2 客队阵容
     * returm bool
     */
    public function LineupDetail(array $array,$id,$home)
    {
        try {
//            将球员的id找出
            $player_id = [];
            foreach ($array as $v){
                $player_id[]=$v['id'];
            }
//            查询数据库
            $modeldata=LineupDetailModel::getInstance()->QueryLineupDetailIn($player_id);

            $lineupdata=[];
            foreach ($array as $value){
                foreach ($modeldata as $v){
                    if ($value['id']==$v['player_id']){
//                        更新
                        $data=[
                            'player_name' =>$value['player_name'],
                            'position'    =>$value['position'],
                            'shirt_number' => $value['shirt_number'],
                            'update_time'  => date('Y-m-d H:i:s'),
                        ];
                        $modeldata=LineupDetailModel::getInstance()->UpdateLineupDetailId($data);
                    }else{
//                        添加数据
                         $value['oss_logo']=OSS::getInstance()->uploadStream('','http://cdn.sportnanoapi.com/football/player/'.$value['logo'],true);
                          $lineupdata[]=[
                              'nami_match_id' => $id,
                              'player_id'     => $value['id'],
                              'player_name'   => $value['name'],
                              'logo'          => $value['oss_logo'],
                              'position'      => $value['position'],
                              'shirt_number'  => $value['shirt_number'],
                              'create_time'   =>date('Y-m-d H:i:s',time()),
                              'update_time'   =>date('Y-m-d H:i:s',time()),
                              'home'          =>$home
                          ];

                    }
                }
            }
            $lineupdetailresult=LineupDetailModel::getInstance()->InsertLineupDetailAll($array);
            if (!$lineupdetailresult) throw new Exception('足球阵容批量插入数据失败');
            Log::log('info','足球详细阵容采集入库成功');
            return $lineupdetailresult;
        }catch(Exception $e){
            Log::error('足球阵容详细错误::'.$e->getMessage());
            return [];
        }


    }
}